home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 2.iso / STUTTGART / PROBLEMS / C / GENERICS next >
Text File  |  1992-04-12  |  8KB  |  227 lines

  1. Article 2083 of comp.sys.acorn:
  2. Path: rusmv1!ira.uka.de!sol.ctr.columbia.edu!zaphod.mps.ohio-state.edu!swrinde!cs.utexas.edu!uunet!mcsun!uknet!ukc!acorn!enevill
  3. From: enevill@acorn.co.uk (Edward Nevill)
  4. Newsgroups: comp.sys.acorn
  5. Subject: New SWI veneers
  6. Message-ID: <10187@acorn.co.uk>
  7. Date: 4 Oct 91 17:11:01 GMT
  8. Sender: enevill@acorn.co.uk
  9. Distribution: comp
  10. Organization: Acorn Computers Ltd, Cambridge, England
  11. Lines: 212
  12.  
  13. Here is an optimised SWI veneer which can be used instead of _kernel_swi.
  14. It is significantly faster than _kernel_swi and is much easier to use (IMHO).
  15.  
  16. Enjoy,
  17. Edward.
  18. --- swiv.h ---
  19. /* SWI veneers:
  20.  *  Written by Edward Nevill and Jonathan Roach in an idle moment between projects.
  21.  */
  22.  
  23. /* Anonymous Error type */
  24. typedef struct Error Error;
  25.  
  26. /* Generic SWI interface
  27.  *  swi(swino,mask,regs...)
  28.  *   swino = SWI number to call as defined in h.swis, X bit set if you wish the
  29.  *           X form of the SWI to be called, clear if you want the non X form.
  30.  *   reg_mask = mask of in / out registers
  31.  *              bits 0-9:   Bit N set => Register N specified on input
  32.  *                          Bit N clear => Register N unspecified on input
  33.  *              bits 22-31: Bit N set => Register N-22 on output stored
  34.  *                              in address specified in varargs list.
  35.  *   ...        In order, input registers followed by output registers,
  36.  *                      starting at r0 and going up.
  37.  *   returns 0 or errorblock pointer if X-bit set
  38.  *   returns r0 if X-bit clear
  39.  *  swix(swino,mask,regs...)
  40.  *   This behaves identically to 'swi' except that it always calls the X form.
  41.  *
  42.  * Eg:
  43.  *   swi(OS_SWINumberToString, IN(R0|R1|R2), n, buf, 255);
  44.  *   e = swi(XOS_SWINumberFromString, IN(R1)|OUT(R0), str, &n);
  45.  *       - Error block pointer (or 0) is returned so must get returned R0
  46.  *       - via argument list.
  47.  *   e = swix(OS_SWINumberFromString, IN(R1)|OUT(R0), str, &n);
  48.  *       - As above but uses the swix function rather that setting the X bit
  49.  *         explicitly (saves one instruction on SWI call).
  50.  *   e = swi(OS_File, IN(R0|R1|R2|R3)|OUT(R4), 255, name, buff, 0, &len);
  51.  *       - We don't care about the load, exec or attrs so don't specify
  52.  *         them in the output registers.
  53.  */
  54.  
  55. extern Error *swix(int swino, int reg_mask, ...);
  56. extern int swi(int swino, int reg_mask, ...);
  57.  
  58. /* Register mask macros
  59.  *  The bits in the register mask are arranged as follows:
  60.  *  31 30 29 ... 22 ...  8 ...  2  1  0
  61.  *  O0 O1 O2 ... O9     I9 ... I2 I1 I0  I(N) = bit set if R(N) used on entry
  62.  *                                       O(N) = bit set if R(N) written on exit
  63.  *  The bits are arranged like this to optimise the case where a SWI is being
  64.  *  called with a small number of input and output registers. For example, a SWI
  65.  *  call which uses R0-R5 on entry and R0-R1 on exit will have a register mask
  66.  *  of 0xC000003f which can be loaded into an ARM register in one instruction
  67.  *  (the compiler performs this optimisation, even when the constant wraps
  68.  *  around between bits 0 and 31). Using the more obvious coding of I0-I9 in bits
  69.  *  0 - 9 and O0-O9 in bits 16-23 leads to a constant of 0x0003003f which require
  70.  *  two instructions.
  71.  */
  72. #define IN(m) (m)
  73. #define OUT(m) ((unsigned)(m&1)<<31|(m&2)<<29|(m&4)<<27|(m&8)<<25|(m&16)<<23|\
  74.                 (m&32)<<21|(m&64)<<19|(m&128)<<17|(m&256)<<15|(m&512)<<13)
  75.  
  76. /* The register names
  77.  *  Change these to use different names if you use R0 - R9 elsewhere in your program
  78.  */
  79. #define R0 0x001
  80. #define R1 0x002
  81. #define R2 0x004
  82. #define R3 0x008
  83. #define R4 0x010
  84. #define R5 0x020
  85. #define R6 0x040
  86. #define R7 0x080
  87. #define R8 0x100
  88. #define R9 0x200
  89. --- swiv.s ---
  90. r0              RN      0
  91. r1              RN      1
  92. r2              RN      2
  93. r3              RN      3
  94. r4              RN      4
  95. r5              RN      5
  96. r6              RN      6
  97. r7              RN      7
  98. r8              RN      8
  99. r9              RN      9
  100. r10             RN      10
  101. r11             RN      11
  102. r12             RN      12
  103. sp              RN      13
  104. lr              RN      14
  105. pc              RN      15
  106.  
  107.  
  108.                 AREA    |C$$code|, CODE, READONLY
  109.  
  110. SWIReturnInst   LDR     pc, [sp, #0*4]
  111.  
  112.         EXPORT  swix
  113. swix    ROUT
  114.         ORR     r0, r0, #&20000
  115.  
  116.         EXPORT  swi
  117. swi     ROUT
  118.  
  119. ; Construct a stack frame that looks something like this:
  120. ;       returnval
  121. ;       LDMIA   r12!, {r0..rn}
  122. ;       SWI     xxxxxx
  123. ;       LDR     pc, [sp]
  124. ;       saved r4-r11,lr
  125. ;       saved r1
  126. ;       saved input values (r2...rn)
  127.  
  128.         STMFD   sp!, {r1-r3}            ; Save r1 and put 1st two variadic args on stack
  129.         BIC     r2, r0, #&ff000000
  130.         ORR     r2, r2, #&ef000000      ; Construct SWI instruction
  131.         ADR     r0, SWIReturn
  132.         BIC     r1, r1, #&ff000000      ; Construct LDMIA R12!, {regs} instruction, if
  133.         BICS    r1, r1, #&00ff0000      ; {regs} = {} (IE no input regs) we must not
  134.         ORRNE   r1, r1, #&e8000000      ; use an LDMIA R12!, {} instruction as this is an
  135.         ORRNE   r1, r1, #&00bc0000      ; invalid instruction, we use a suitable NOP instead
  136.         MOVEQ   r1, #0                  ; 0 = opcode for ANDEQ r0, r0, r0 (a suitable NOP)
  137.         LDR     r3, SWIReturnInst
  138.         STMFD   sp!, {r0-r9,r11,lr}     ; Save regs and set up SWI call routine (in R0-R3)
  139.         ADD     r12, sp, #(12+1)*4      ; Point R12 at input regs on stack.
  140.         ADD     pc, sp, #4              ; Call routine on stack
  141. SWIReturn
  142.         LDR     lr, [sp, #(12+0)*4]     ; Fetch reg mask again
  143.         MOVS    lr, lr, ASL #1          ; Shift out setting C if R0 to be written, N
  144.         LDRCS   r11, [r12], #4          ; if R1 to be written.
  145.         STRCS   r0, [r11]
  146.         LDRMI   r11, [r12], #4
  147.         STRMI   r1, [r11]
  148.         MOVS    lr, lr, ASL #2          ; Shift 2 bits each time for the next 2 regs
  149.         LDRCS   r11, [r12], #4
  150.         STRCS   r2, [r11]
  151.         LDRMI   r11, [r12], #4
  152.         STRMI   r3, [r11]
  153.         MOVS    lr, lr, ASL #2
  154.         LDRCS   r11, [r12], #4
  155.         STRCS   r4, [r11]
  156.         LDRMI   r11, [r12], #4
  157.         STRMI   r5, [r11]
  158.         MOVS    lr, lr, ASL #2
  159.         LDRCS   r11, [r12], #4
  160.         STRCS   r6, [r11]
  161.         LDRMI   r11, [r12], #4
  162.         STRMI   r7, [r11]
  163.         MOVS    lr, lr, ASL #2
  164.         LDRCS   r11, [r12], #4
  165.         STRCS   r8, [r11]
  166.         LDRMI   r11, [r12], #4
  167.         STRMI   r9, [r11]
  168.         LDR     r1, [sp, #2*4]
  169.         TST     r1, #&20000             ; X-bit clear
  170.         CMPEQ   pc, #&80000000          ; SET V flag if so, so R0 not cleared
  171.         MOVVC   r0, #0                  ; Clear R0 if no error (or X-bit clear)
  172.         ADD     sp, sp, #4*4            ; Drop SWI call routine
  173.         LDMIA   sp!, {r4-r9,r11,lr}
  174.         ADD     sp, sp, #3*4            ; Drop saved R1 and 1st two variadic args.
  175.         MOVS    pc, lr
  176.  
  177.         END
  178. --- switime.c ---
  179. /* This program times the 'swi' and '_kernel_swi' veneers. It calls the X form of
  180.  * OS_GenerateError (the fastest possible swi since all it does is set the V flag).
  181.  * It calls it with a sample register set which would be used to call XOS_ReadVarVal.
  182.  *
  183.  * IE. It sets up the registers as though it were calling XOS_ReadVarVal and then
  184.  * calls a trivial SWI (XOS_GenerateError) so that it only measure the overhead of
  185.  * calling a typical SWI.
  186.  *
  187.  * Timings for an A540 in mode 0
  188.  *                  swi      _kernel_sw
  189.  *  Cache enabled   168 csec 252 csec
  190.  *  Cache disabled  340 csec 497 csec
  191.  *
  192.  * It also serves as an example of how much easier the 'swi' veneer is to use. Note the
  193.  * 6 or 7 lines required using '_kernel_swi' compared with the one line using 'swi'.
  194.  */
  195. #include <stdio.h>
  196.  
  197. #include "kernel.h"
  198. #include "swiv.h"
  199. #include "swis.h"
  200.  
  201. int main(void)
  202. {
  203.     _kernel_swi_regs r;
  204.     int t1, t2, t3;
  205.     int i;
  206.     int exists;
  207.  
  208.     t1 = swi(OS_ReadMonotonicTime, 0);
  209.     for (i = 0; i < 100000; i++)
  210.         swix(OS_GenerateError, IN(R0|R2|R3|R4)|OUT(R2), "ADFSFiler$Path", -1, 0, 0, &exists);
  211.     t2 = swi(OS_ReadMonotonicTime, 0);
  212.     for (i = 0; i < 100000; i++) {
  213.         r.r[0] = (int)"ADFSFiler$Path";
  214.         r.r[2] = -1;
  215.         r.r[3] = 0;
  216.         r.r[4] = 0;
  217.         _kernel_swi(OS_GenerateError, &r, &r);
  218.         exists = r.r[2];
  219.     }
  220.     t3 = swi(OS_ReadMonotonicTime, 0);
  221.     printf("Times:-\nswix = %d csec\n_kernel_swi = %d csec\n", t2-t1, t3-t2);
  222.     return 0;
  223. }
  224. ---
  225.  
  226.  
  227.